package drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.branch_transaction_status_report;

import drds.global_transaction.api.api.BranchTransactionStatus;
import drds.global_transaction.api.api.RpcContext;
import drds.global_transaction.api.message_codec.Message;
import drds.global_transaction.api.message_codec.request_and_response.transaction.TransactionResponse;
import drds.global_transaction.api.message_codec.request_and_response.transaction.global_transaction.GlobalTransactionRequestMessage;
import lombok.Getter;
import lombok.Setter;

import java.nio.ByteBuffer;

public class BranchTransactionStatusReportRequestMessage extends GlobalTransactionRequestMessage
{
	private static final long serialVersionUID = 1L;

	/**
	 * 需要结合当前的地址和端口 才是一个完整的全局事务ID
	 */
	@Setter
	@Getter
	private long globaltransactionId;
	@Setter
	@Getter
	private long branchTransactionId;
	@Setter
	@Getter
	private String resourceId;
	@Setter
	@Getter
	private BranchTransactionStatus branchTransactionStatus;
	@Setter
	@Getter
	private String applicationData;

	@Override
	public void decode(ByteBuffer byteBuffer)
	{
		this.globaltransactionId = byteBuffer.getLong();
		this.branchTransactionId = byteBuffer.getLong();
		this.branchTransactionStatus = BranchTransactionStatus.get(byteBuffer.get());
		short length = byteBuffer.getShort();
		if (length > 0)
		{
			byte[] bytes = new byte[length];
			byteBuffer.get(bytes);
			this.resourceId = new String(bytes, Message.UTF8);
		}

		int $length = byteBuffer.getInt();
		if ($length > 0)
		{
			byte[] bytes = new byte[$length];
			byteBuffer.get(bytes);
			this.applicationData = new String(bytes, Message.UTF8);
		}
	}

	@Override
	public short getTypeCode()
	{
		return Message.branch_transaction_status_report_request_message;
	}

	@Override
	public TransactionResponse handle(RpcContext rpcContext)
	{
		return transactionCoordinatorInboundHandler.handle(rpcContext, this);
	}

	@Override
	public ByteBuffer putBytesToByteBuffer()
	{
		ByteBuffer byteBuffer = createByteBuffer();
		byte[] applicationDataBytes = null;
		if (this.applicationData != null)
		{
			applicationDataBytes = applicationData.getBytes(Message.UTF8);
			if (applicationDataBytes.length > 512)
			{
				byteBuffer = ByteBuffer.allocate(applicationDataBytes.length + 1024);
			}
		}

		// 1. Transaction Id
		byteBuffer.putLong(this.globaltransactionId);
		// 2. Branch Id
		byteBuffer.putLong(this.branchTransactionId);
		// 3. Branch Status
		byteBuffer.put((byte) this.branchTransactionStatus.ordinal());
		// 4. Resource Id
		if (this.resourceId != null)
		{
			byte[] bytes = resourceId.getBytes(Message.UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		// 5. Application Data
		if (this.applicationData != null)
		{
			byteBuffer.putInt(applicationDataBytes.length);
			if (applicationDataBytes.length > 0)
			{
				byteBuffer.put(applicationDataBytes);
			}
		} else
		{
			byteBuffer.putInt(0);
		}
		markAsEncode();
		return byteBuffer;
	}
}
